package com.example.javatool.recallfile;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FileRenamer {

    /**
     * 读取 mail_file_belong.txt 文件并构建映射关系
     * @param filePath mail_file_belong.txt 文件的路径
     * @return 包含文件名对应替换内容的映射
     * @throws IOException 当文件读取出错时抛出
     */
    private static Map<String, String> readMailFileBelong(String filePath) throws IOException {
        Map<String, String> map = new HashMap<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(" ");
                if (parts.length >= 2) {
                    map.put(parts[0], parts[1]);
                }
            }
        }
        return map;
    }

    /**
     * 重命名指定文件夹下的文件
     * @param directoryPath 文件夹路径
     * @param mailFileBelongPath mail_file_belong.txt 文件的路径
     * @throws IOException 当文件操作出错时抛出
     */
    public static void renameFiles(String directoryPath, String mailFileBelongPath,String splitChar) throws IOException {
        Map<String, String> replaceMap = readMailFileBelong(mailFileBelongPath);
        File directory = new File(directoryPath);
        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isFile()) {
                        String originalName = file.getName();
                        for (Map.Entry<String, String> entry : replaceMap.entrySet()) {
                            String key = entry.getKey();
                            String value = entry.getValue();
                            if (originalName.contains(key)) {
                                // 指定分隔符，这里以空格为例，可按需修改
                                String[] parts = value.split(splitChar);
                                StringBuilder newNameBuilder = new StringBuilder(originalName);
                                for (String part : parts) {
                                    if (!originalName.contains(part)) {
                                        if (newNameBuilder.length() > 0) {
                                            newNameBuilder.insert(0, "-");
                                        }
                                        newNameBuilder.insert(0, part);
                                    }
                                }
                                String newName = newNameBuilder.toString();
                                File newFile = new File(file.getParent(), newName);
                                if (file.renameTo(newFile)) {
                                    System.out.println("文件 " + originalName + " 已重命名为 " + newName);
                                } else {
                                    System.out.println("重命名文件 " + originalName + " 失败");
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        String directoryPath = "D:\\email\\完成\\附件";
        String mailFileBelongPath = "D:\\email\\mail_file_belong.txt";
        String splitChar = "_";
        try {
            renameFiles(directoryPath, mailFileBelongPath,splitChar);
            System.out.println("文件重命名操作完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
